/*! screenlog - v0.2.2 - 2016-07-11
* https://github.com/chinchang/screenlog.js
* Copyright (c) 2016 Kushagra Gour; Licensed  */
(function() {
    var logEl, isInitialized = false, _console = {};
    _options = {
        bgColor: "#222c3c",
        logColor: "lightgreen",
        infoColor: "blue",
        warnColor: "orange",
        errorColor: "red",
        freeConsole: false,
        css: "",
        autoScroll: true
    };
    function createElement(tag, css) {
        var element = document.createElement(tag);
        element.className = "screen";
        element.style.cssText = css;
        return element
    }
    function createPanel() {
        var div = createElement("div", "z-index:99;font-family:PingFang-SC-Bold;font-size:10px;font-weight:bold;text-align:left;width:100%;height:150px;overflow:auto;background:" + _options.bgColor + ";" + _options.css);

        var screenHeader = createElement("div", "font-family:MicrosoftYaHeiUI;line-height:27px;color:#fff;border-bottom:2px solid #222c3c;border-top:2px solid #222c3c;height:28px;background:#1c2531;");
        screenHeader.className = "screenheader";
        var log = createElement("div", "text-align: center;width: 62px;font-size: 12px;font-weight: 100;background:" + _options.bgColor + ";border-bottom: 1px solid " + _options.bgColor);
        log.innerHTML = "日志";
        screenHeader.appendChild(log);
        div.appendChild(screenHeader);
        var screenContent = createElement("div", "height: calc(100% - 44px);overflow: auto;");
        screenContent.className = "screenContent";
        div.appendChild(screenContent);
        return div
    }
    function genericLogger(color) {
        var screenContent = logEl.querySelector(".screenContent");
        return function() {
            var el = createElement("div", "line-height:17px;min-height:17px;padding:0 5px 0 17px;word-break: break-all;background:" + (screenContent.children.length % 2 ? "rgba(255,255,255,0.1)" : "") + ";color:" + color);
            var val = [].slice.call(arguments).reduce(function(prev, arg) {
                return prev + " " + (typeof arg === "object" ? JSON.stringify(arg) : arg)
            }, "");
            if (val.indexOf("THREE") == -1) {
                el.textContent = val;
                screenContent.appendChild(el);
                if (_options.autoScroll) {
                    screenContent.scrollTop = screenContent.scrollHeight - screenContent.clientHeight
                }
            } else {}
        }
    }
    function clear() {
        logEl.querySelector(".screenContent").innerHTML = ""
    }
    function log() {
        return genericLogger(_options.logColor).apply(null, arguments)
    }
    function info() {
        return genericLogger(_options.infoColor).apply(null, arguments)
    }
    function warn() {
        return genericLogger(_options.warnColor).apply(null, arguments)
    }
    function error() {
        return genericLogger(_options.errorColor).apply(null, arguments)
    }
    function setOptions(options) {
        for (var i in options) {
            if (options.hasOwnProperty(i) && _options.hasOwnProperty(i)) {
                _options[i] = options[i]
            }
        }
    }
    function init(options) {
        if (isInitialized) {
            return
        }
        isInitialized = true;
        if (options) {
            setOptions(options)
        }
        logEl = createPanel();
        options.container.appendChild(logEl)
        if (!_options.freeConsole) {
            _console.log = console.log;
            _console.clear = console.clear;
            _console.info = console.info;
            _console.warn = console.warn;
            _console.error = console.error;
            console.log = originalFnCallDecorator(log, "log");
            console.clear = originalFnCallDecorator(clear, "clear");
            console.info = originalFnCallDecorator(info, "info");
            console.error = originalFnCallDecorator(error, "error")
        }
    }
    function destroy() {
        isInitialized = false;
        console.log = _console.log;
        console.clear = _console.clear;
        console.info = _console.info;
        console.warn = _console.warn;
        console.error = _console.error;
        logEl.remove()
    }
    function checkInitialized() {
        if (!isInitialized) {
            throw "You need to call `screenLog.init()` first."
        }
    }
    function checkInitDecorator(fn) {
        return function() {
            checkInitialized();
            return fn.apply(this, arguments)
        }
    }
    function originalFnCallDecorator(fn, fnName) {
        return function() {
            fn.apply(this, arguments);
            if (typeof _console[fnName] === "function") {
                _console[fnName].apply(console, arguments)
            }
        }
    }
    window.screenLog = {
        init: init,
        log: originalFnCallDecorator(checkInitDecorator(log), "log"),
        clear: originalFnCallDecorator(checkInitDecorator(clear), "clear"),
        info: originalFnCallDecorator(checkInitDecorator(clear), "info"),
        warn: originalFnCallDecorator(checkInitDecorator(warn), "warn"),
        error: originalFnCallDecorator(checkInitDecorator(error), "error"),
        destroy: checkInitDecorator(destroy)
    }
}
)();
